package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.adapter.ShowLogeAdapter;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.inter.OnMouseListItemClickListener;
import com.hzdesk.media.koreabatchmatvector.logic.contract.ShowLogeContract;
import com.hzdesk.media.koreabatchmatvector.receiver.SDCardReceiver;
import com.hzdesk.media.koreabatchmatvector.utils.DeleteDirTest;
import com.hzdesk.media.koreabatchmatvector.utils.SharedPreferencesUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/25.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class ShowLogePersenter implements ShowLogeContract.IShowLogePersenter {
    private ShowLogeContract.IShowLogeView mIShowLogeView;

    private ShowLogeAdapter adapter;
    private List<String> mlist;

    public ShowLogePersenter(ShowLogeContract.IShowLogeView mIShowLogeView) {
        this.mIShowLogeView = mIShowLogeView;
    }
    @Override
    public void RegisterReceiver(Context mContext) {

    }

    @Override
    public void UnregisterReceiver(Context mContext) {

    }

    @Override
    public void initRecyclerView(RecyclerView myRecyclerView, final Context mContext) {
          //创建LinearLayoutManager 对象 这里使用 LinearLayoutManager 是线性布局的意思
        GridLayoutManager layoutmanager = new GridLayoutManager(mContext,3);
        layoutmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //设置RecyclerView 布局
        myRecyclerView.setLayoutManager(layoutmanager);
        //设置Adapter
        adapter = new ShowLogeAdapter(mContext);
        myRecyclerView.setAdapter(adapter);
        adapter.setmOnMouseListItemClickListener(new OnMouseListItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                if(mlist != null && mlist.size() >0){
                    String path = mlist.get(position);
                    if(path != null && !TextUtils.isEmpty(path)){
                        Log.e("ShowLogePersenter", "OnItemClick: "+path );
                        SharedPreferencesUtils sharedPreferencesUtils = new SharedPreferencesUtils(mContext);
                        sharedPreferencesUtils.putDate(AppConfig.WATER_MARK_PATH,path);
                    }
                }
            }
        });

    }

    @Override
    public void GetWatermarkFileDate(Context mContext) {
        // 读取 sdCard    Watermark/watermark/ 目录下文件路劲显示水印
        // todo 遍历水印为文件夹得到水印文件路径集合
        mlist = getWaterMark();
        // todo 适配显示在RecyclerView上
        adapter.AddDate(mlist);
    }

    @NonNull
    private List<String> getWaterMark() {
        List<String> mlist = new ArrayList<>();
        String path = DeleteDirTest.getPath("Watermark/watermark/");
        File[] files = new File(path).listFiles();
        for (File file : files) {
            if(file.isFile() && file.exists()){
                mlist.add(file.getAbsolutePath());
            }
        }
        return mlist;
    }


}
